package com.yh.controller;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.apache.commons.io.FileUtils;
import org.apache.commons.io.IOUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import com.yh.bean.Emp;
import com.yh.bean.t_class;
import com.yh.bean.t_coll;
import com.yh.bean.t_major;
import com.yh.bean.t_stu;
import com.yh.service.EmpServiceImpl;

@Controller
@RequestMapping("/emp")
public class EmpController {

	@Autowired
	private EmpServiceImpl empServiceImpl;

	@RequestMapping("/list")
	@ResponseBody
	public Map queryEmp(int currentPage) {
		Map map = empServiceImpl.queryEmp(currentPage);
	
		System.out.println(map.get("list"));
		return map;
	}

	@RequestMapping("/update")
	public String updateEmp(Model model) {
		empServiceImpl.updateEmp();

		return "update";
	}

	@RequestMapping("/scoll")
	@ResponseBody
	public List<t_coll> searchColl() {
		List<t_coll> collList = empServiceImpl.searchColl();
	
		return collList;
	}

	@RequestMapping("/major")
	@ResponseBody
	public List<t_major> searchMajor(int collId) {
		List<t_major> majorList = empServiceImpl.searchMajor(collId);

		return majorList;
	}

	@RequestMapping("/cls")
	@ResponseBody
	public List<t_class> searchClass(int majorId) {
		List<t_class> classList = empServiceImpl.searchClass(majorId);
	
		return classList;
	}

	@RequestMapping("/addstu")
	public String addstu(@RequestParam("myphoto")MultipartFile file, t_stu stu, HttpServletRequest request) {

		System.out.println("学院的id是======="+stu.getTc().getT_major().getT_coll().getCollId());
		System.out.println("专业id是======="+stu.getTc().getT_major().getMajorId());
		System.out.println("班级的id======="+stu.getTc().getClassId());
		
		// 获取上传的目标文件夹目录
		String realPath = request.getServletContext().getRealPath("images")+"/";
	
		// 获取文件名
		String filename = file.getOriginalFilename();
		// 生成新文件名
		Date date = new Date();
		SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
		String newfilename = realPath + sdf.format(date) + filename;
		System.out.println(newfilename);
		File destfile = new File(newfilename);
		try {// 复制文件
			FileUtils.copyInputStreamToFile(file.getInputStream(), destfile);
		} catch (IOException e) {

			e.printStackTrace();
		}
		
	 //生成学号 学号以当前时间+专业id+班级id
		String stuId=sdf.format(date)+stu.getTc().getT_major().getMajorId()+stu.getTc().getClassId();
		 System.out.println("学号是===="+stuId);
		stu.setStuId(stuId);
		//存储照片的地址
		stu.setStuPhoto(sdf.format(date)+filename);;
       System.out.println(sdf.format(date)+filename);
       //新增的学生数
       int a=empServiceImpl.addStu(stu);
       System.out.println("增加的学生数"+a);
       System.out.println("新增成功！！！");
		return "success";
	}

}
